package com.netease.examination.web.buyer;

import com.netease.examination.dto.BuyerGoods;
import com.netease.examination.dto.ResultDTO;
import com.netease.examination.model.Goods;
import com.netease.examination.service.buyer.BuyerService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * designed by destiny
 *
 * @author destiny
 *         e-mail destinywk@163.com
 *         github https://github.com/DestinyWang
 *         oschina https://git.oschina.net/destinywk
 * @version JDK 1.8.0_101
 * @since 2017/3/30 22:16
 */
@Controller
public class BuyerController {

    @Resource
    private BuyerService buyerService;

    @RequestMapping(value = "/findGoodsList", produces = "application/json; charset=utf-8",
            method = {RequestMethod.GET, RequestMethod.POST})
    @CrossOrigin
    @ResponseBody
    public ResultDTO findGoodsList() {
        List<Goods> goodsList = buyerService.findGoodsList();
        ResultDTO<List<Goods>> resultDTO = new ResultDTO<List<Goods>>(true, goodsList);
        return resultDTO;
    }

    @RequestMapping(value = "/findUnbuyGoodsList", method = {RequestMethod.GET, RequestMethod.POST})
    @CrossOrigin
    @ResponseBody
    public ResultDTO findUnbuyGoodsList() {
        List<Goods> goodsList = buyerService.findUnbuyGoodsList();
        ResultDTO<List<Goods>> resultDTO = new ResultDTO<>(true, goodsList);
        return resultDTO;
    }

    @RequestMapping(value = "/findGoodsDetail", method = {RequestMethod.GET, RequestMethod.POST})
    @CrossOrigin
    @ResponseBody
    public ResultDTO findGoodsDetail(@RequestParam("goodId") Long goodId) {
        Goods goods = buyerService.findGoodsDetail(goodId);
        ResultDTO<Goods> resultDTO = new ResultDTO<Goods>(true, goods);
        return resultDTO;
    }

    @RequestMapping(value = "/putIntoShoppingCar", method = {RequestMethod.GET, RequestMethod.POST})
    @CrossOrigin
    @ResponseBody
    public void putIntoShoppingCar(@RequestParam("count") Integer count, @RequestParam("goodsId") Long goodsId) {
        BuyerGoods buyerGoods = new BuyerGoods();
        buyerGoods.setGoodsId(goodsId);
        buyerGoods.setCount(count);
        buyerService.putIntoShoppingCar(buyerGoods);
    }

    @RequestMapping(value = "/findShoppingCar", method = {RequestMethod.GET, RequestMethod.POST})
    @CrossOrigin
    @ResponseBody
    public ResultDTO findShoppingCar() {
        List<BuyerGoods> buyerGoodsList = buyerService.findShoppingCar();
        ResultDTO<List<BuyerGoods>> resultDTO = new ResultDTO<List<BuyerGoods>>(true, buyerGoodsList);
        return resultDTO;
    }

    @RequestMapping(value = "/settleAccounts", method = {RequestMethod.GET, RequestMethod.POST})
    @CrossOrigin
    @ResponseBody
    public void settleAccounts() {
        buyerService.settleAccounts();
    }

    @RequestMapping(value = "/exitShoppingCar", method = {RequestMethod.GET, RequestMethod.POST})
    @CrossOrigin
    @ResponseBody
    public void exitShoppingCar() {
        buyerService.exitShoppingCar();
    }

    @RequestMapping(value = "/findHistoryOrder", method = {RequestMethod.GET, RequestMethod.POST})
    @CrossOrigin
    @ResponseBody
    public ResultDTO findHistoryOrder() {
        List<BuyerGoods> buyerGoodsList = buyerService.findHistoryOrder();
        ResultDTO<List<BuyerGoods>> resultDTO = new ResultDTO<List<BuyerGoods>>(true, buyerGoodsList);
        return resultDTO;
    }

}
